<template>
	<view>
		<view style="padding: 40rpx;">
			<input v-model="formData.title" style="font-size: 32rpx;padding: 0 !important;" type="text" name="" id="" placeholder="添加问题标题">
		</view>
		<view style="border-top: 2rpx #999 dashed;margin:0 44rpx;">
		</view>
		<view style="padding: 40rpx;">
			<textarea v-model="formData.content" style="font-size: 32rpx;" type="text" name="" id="" placeholder="输入您的意见">
			</textarea>
		</view>
		<view class="upload" style="margin-top: 200rpx;padding: 0 40rpx;">
			<view style="color:#333;font-size: 32rpx;margin-bottom: 40rpx;">上传图片</view>
			<u-upload width="200rpx" height="200rpx" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic"
				name="1" :maxCount="1">
			</u-upload>
		</view>
		<u-button @tap="submit" type="primary" color="red" class="custom-style" text="确认"></u-button>
	</view>
</template>

<script>
	import baseURL from "@/config/utils.js"
	export default {
		data() {
			return {
				fileList1: [],
				formData: {
					content: "",
					title: ""
				}
			}
		},
		methods: {
			async submit() {
				console.log(this.fileList1);
				let formData = uni.$u.deepClone(this.formData)
				formData.imageUrl = this.fileList1[0] && this.fileList1[0].url;
				if(!formData.title){
					uni.showToast({
						title:"标题不能为空！",
						icon:"none"
					})
					return
				}
				if(!formData.content){
					uni.showToast({
						title:"内容不能为空！",
						icon:"none"
					})
					return
				}

				let data = await this.$api.addFeedbackApi(formData)
				if (data.code == 200) {
					uni.showToast({
						title: "提交成功，感谢您的反馈！",
						icon: "none",
						duration:1000
					})
					//this.$u.sleep(1000).then(() => {
						uni.navigateBack()
					//})

				} else {
					uni.showToast({
						title: "未知错误！",
						icon: "none"
					})
				}
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let token = uni.getStorageSync('token')
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseURL.baseURL + '/app/upload/uploadOss', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							'X-Access-Token': token
						},
						formData: {
							type: 'feedback'
						},
						success: (res) => {
							resolve(JSON.parse(res.data).result)
						}
					});
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-icon__icon {
		font-size: 80rpx !important;
	}

	.custom-style {
		position: fixed;
		bottom: 80rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 90%;
	}
</style>
